import javax.swing.JPanel;

import javax.swing.border.LineBorder;

import org.json.JSONObject;

import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.DefaultComboBoxModel;
import java.awt.Font;
import javax.swing.ButtonGroup;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.awt.event.ActionEvent;

public class ImportData_Room extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField TF_dir;
	private JTextField TF_room_name;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextField TF_note;
	private JLabel L_room_name;
	private JPanel P_one;
	private JLabel L_sex;
	private JRadioButton RB_boy;
	private JRadioButton RB_girl;
	private JLabel L_size;
	private	JComboBox<String> comboBox;
	private JPanel P_group;
	private JButton B_choose_dir;
	private JButton B_import_group;
	private JButton B_import_one;
	private JLabel L_note;
	private boolean check_room(String str){
		try{
			JSONObject obj = new JSONObject(new SocketOp().request("{\"method\":\"get_room\",\"room_name\":\""+str+"\"}"));
			if(obj.has("room_name")&&obj.getString("room_name").equals(str)){
				return false;//已存在
			}else{
				return true;//不存在
			}
		}catch(Exception e){
			return true;//不存在
		}
		
	}
	private boolean check_file_name(String str){
		for(int i=0;i<str.length();i++)
		{
			if(Character.isDigit(str.charAt(i))||Character.isLetter(str.charAt(i))||str.charAt(i)=='-'||str.charAt(i)=='_'){
				continue;
			}else{
				return false;
			}
		}
		return true;
	}
	private void check_dir(String dirStr){
		int jsonFiles = 0;
		int importFiles = 0;
		try{
			File[] fs = new File(dirStr).listFiles();
			for(int i=0;i<fs.length;i++){
				try{
					if(fs[i].isFile()){
						String fileName = fs[i].getAbsolutePath();
						if(fileName.substring(fileName.lastIndexOf('.')+1).equals("json")){
							jsonFiles ++;
							if(new File(fileName).canRead()){
								BufferedReader in = new BufferedReader(new FileReader(fileName));
								String sendMsg = in.readLine();
								while(in.ready()){
									sendMsg += in.readLine();
								}
								JSONObject obj = new JSONObject(sendMsg);
								if(obj.has("room_name")&&obj.has("room_sex")&&obj.has("room_size")&&check_file_name(obj.getString("room_name"))&&check_room(obj.getString("room_name"))){
									obj.put("method", "import_room");
									if(new JSONObject(new SocketOp().request(obj.toString())).get("method").equals("import_room")) {
										importFiles++;
									}
								}
								in.close();
							}
						}
					}
				}catch(Exception e1){
					
				}
			}
		}catch(Exception e){
			
		}
		JOptionPane.showMessageDialog(null, "当期目录下共有JSON文件:"+jsonFiles+"个\n成功导入宿舍数据:"+importFiles+"个");
	}

	public ImportData_Room() {

		setBorder(new LineBorder(Color.LIGHT_GRAY));
		setLayout(null);
		
		P_one = new JPanel();
		P_one.setBorder(new LineBorder(Color.LIGHT_GRAY));
		P_one.setBounds(21, 38, 441, 144);
		add(P_one);
		P_one.setLayout(null);
		
		
		
		
		L_room_name = new JLabel("寝室号");
		L_room_name.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		L_room_name.setHorizontalAlignment(SwingConstants.RIGHT);
		L_room_name.setBounds(128, 10, 54, 15);
		P_one.add(L_room_name);
		
		TF_room_name = new JTextField();
		TF_room_name.setBounds(225, 7, 88, 21);
		P_one.add(TF_room_name);
		TF_room_name.setColumns(10);
		
		L_sex = new JLabel("性别");
		L_sex.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		L_sex.setHorizontalAlignment(SwingConstants.RIGHT);
		L_sex.setBounds(128, 35, 54, 15);
		P_one.add(L_sex);
		
		L_size = new JLabel("容量");
		L_size.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		L_size.setHorizontalAlignment(SwingConstants.RIGHT);
		L_size.setBounds(128, 60, 54, 15);
		P_one.add(L_size);
		
		RB_boy = new JRadioButton("男");
		buttonGroup.add(RB_boy);
		RB_boy.setSelected(true);
		RB_boy.setBounds(225, 31, 43, 23);
		P_one.add(RB_boy);
		
		RB_girl = new JRadioButton("女");
		buttonGroup.add(RB_girl);
		RB_girl.setBounds(270, 31, 43, 23);
		P_one.add(RB_girl);
		
		comboBox = new JComboBox<String>();
		comboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8"}));
		comboBox.setSelectedIndex(4);
		comboBox.setBounds(225, 57, 88, 21);
		P_one.add(comboBox);
		
		P_group = new JPanel();
		P_group.setBorder(new LineBorder(Color.LIGHT_GRAY));
		P_group.setBounds(21, 192, 441, 144);
		add(P_group);
		P_group.setLayout(null);
		
		
		TF_dir = new JTextField();
		TF_dir.setBounds(10, 66, 306, 21);
		P_group.add(TF_dir);
		TF_dir.setColumns(10);
		
		B_choose_dir = new JButton("选择文件夹");
		B_choose_dir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser jFileChooser = new JFileChooser();
				jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				jFileChooser.showOpenDialog(jFileChooser);
				try{
					TF_dir.setText(jFileChooser.getSelectedFile().getAbsolutePath());
				}catch(Exception e1){}
				
			}
		});
		B_import_group = new JButton("批量导入");
		B_import_group.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(TF_dir.getText().equals(""))
					JOptionPane.showMessageDialog(null, "没有设置目录");
				else{
					check_dir(TF_dir.getText());
				}
					
			}
		});
		B_import_group.setBounds(175, 111, 93, 23);
		P_group.add(B_import_group);
		
		L_note = new JLabel("备注");
		L_note.setHorizontalAlignment(SwingConstants.RIGHT);
		L_note.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		L_note.setBounds(128, 85, 54, 15);
		P_one.add(L_note);
		
		TF_note = new JTextField();
		TF_note.setBounds(225, 83, 134, 21);
		P_one.add(TF_note);
		TF_note.setColumns(10);
		
		B_choose_dir.setBounds(326, 65, 105, 23);
		P_group.add(B_choose_dir);
		B_import_one = new JButton("单个导入");
		B_import_one.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String sendMsg = "{\"method\":\"import_room\",\"room_name\":\""+TF_room_name.getText()+"\",\"room_sex\":\""+(RB_boy.isSelected()?"boy":"girl")+"\",\"room_size\":"+comboBox.getSelectedIndex()+",\"room_note\":\""+TF_note.getText()+"\"}";
				if(check_file_name(TF_room_name.getText()))	{
					if(check_room(TF_room_name.getText()))
					{
						try{
							JSONObject obj = new JSONObject(new SocketOp().request(sendMsg));
							if(obj.has("result")&&obj.getString("result").equals("success")){
								JOptionPane.showMessageDialog(null, "成功导入一个数据","导入成功",JOptionPane.INFORMATION_MESSAGE);
								
							}else{
								JOptionPane.showMessageDialog(null, "未知错误","导入失败",JOptionPane.ERROR_MESSAGE);
							}
						}catch(Exception e1){
							JOptionPane.showMessageDialog(null, "未知错误","导入失败",JOptionPane.ERROR_MESSAGE);
						}
					}else{
						try{
							
							JOptionPane.showMessageDialog(null, "已经存在一个同名寝室，请选择其他的寝室名","导入失败",JOptionPane.ERROR_MESSAGE);
						}catch(Exception e1){
							
						}
					}
				}else{
					JOptionPane.showMessageDialog(null, "寝室名仅包含数字、字母 、\"-\"、\"_\"","格式",JOptionPane.ERROR_MESSAGE);
				}
				
			}
		});
		B_import_one.setBounds(175, 111, 93, 23);
		P_one.add(B_import_one);
		
	}
}
